import type { Menu } from '@/components/layout/SubMenu/interface'
import { useMenuStore } from '@/stores/modules/menu'
import { getShowMenuList } from '@/utils'

export const useGlobalStore = defineStore(
  'global',
  () => {
    // token
    const token = ref()
    const setToken = (val: string) => {
      token.value = val
    }
    const clearToken = () => {
      token.value = undefined
    }

    // 菜单列表
    const menuStore = useMenuStore()
    const menuList = ref<Menu.MenuOptions[]>([])
    watch(menuList, (val) => {
      menuStore.setMenuMap(val)
    })
    const setMenuList = async (val: Menu.MenuOptions[]) => {
      menuList.value = val
    }
    const showMenuList = () => getShowMenuList(menuList.value)

    // 当前路由路径 用于按钮权限指令使用
    const routePath = ref('')
    const setRoutePath = (val: string) => {
      routePath.value = val
    }

    return {
      token,
      setToken,
      clearToken,
      menuList,
      setMenuList,
      showMenuList,
      routePath,
      setRoutePath
    }
  },
  {
    persist: true
  }
)
